import { ref, reactive } from 'vue'
import { defineStore } from 'pinia'
import {
  httpGetFolderList,
  httpgetShortCutList,
  httpNoteList,
  httpNoteListWithLabel,
} from '@/api/note'
// import { generateTree } from '@/utils/tools'
const useNoteStore = defineStore('note', () => {
  const noteData = ref([
    {
      id: 0,
      pid: null,
      label: '全部笔记',
      icon: 'Notebook',
      level: 1,
      children: [],
      type: 1,
    },
  ]) // 所有文件
  const selectedFolder = ref({
    id: 0,
    label: '全部笔记',
    type: 1,
  }) // 设置选中的文件夹
  const selectedTag = ref() // 设置选中的文件夹
  // const breadCrumbList = ref([]) // 面包屑
  const shortCutData = ref([
    {
      id: 1,
      label: '快捷方式',
      icon: 'Paperclip',
      children: [],
    },
  ]) // 所有快捷方式

  const tagList = ref([]) // 左侧标签列表

  const noteList = ref([]) // 右侧笔记列表
  const noteListTotal = ref(0)
  const loading = ref(false)
  const wangEditorRef = ref(null)

  const noReadNum = ref(0) // 未读数量
  // 设置选中的文件夹
  function setSelectedFolder(data) {
    selectedFolder.value = data
  }
  // 设置选中的标签
  function setSelectedTag(data) {
    selectedFolder.value = data
  }

  function addIdsToParent(node, parentLevel) {
    if (!node.childIds) {
      node.childIds = []
    }
    node.childIds.push(node.id)
    node.level = parentLevel ? ++parentLevel : 1
    node.deepLevel = node.level ? node.level : 1
    if (node.children && node.children.length > 0) {
      node.children.forEach((child) => {
        addIdsToParent(child, node.level)
        node.childIds.push(...child.childIds)
        node.deepLevel = Math.max(node.deepLevel, child.deepLevel)
      })
    }
  }
  // 获取所有文件夹列表
  const getFolderListData = async () => {
    const res = await httpGetFolderList()
    const { list } = res
    if (list.length) {
      noteData.value = list
      noteData.value.forEach((node) => {
        addIdsToParent(node)
      })
      noteData.value && (noteData.value[0].label = '全部笔记')
    }
  }
  // 获取所有快捷方式
  const getShortCutList = async () => {
    const res = await httpgetShortCutList()
    const { list } = res
    if (list.length) {
      shortCutData.value[0].children = [
        ...list[0].folder,
        ...list[0].note,
        ...list[0].label,
      ]
      shortCutData.value[0].children.forEach((item) => {
        item.top = true
      })
    }
  }
  const queryNote = reactive({
    page: 1,
    limit: 20,
    folder_id: 1,
    keyword: '',
    sort_order: 1,
  })
  const getList = async (showLoading = true) => {
    if (showLoading) loading.value = true
    let response
    if (
      (selectedFolder.value && selectedFolder.value.type === 1) ||
      (!selectedFolder.value && !selectedTag.value)
    ) {
      let data = {
        ...queryNote,
      }
      selectedFolder.value && (data.folder_id = selectedFolder.value.id)
      response = await httpNoteList(data).catch(() => null)
    }
    if (selectedTag.value) {
      let data = {
        ...queryNote,
        label_id: selectedTag.value.id,
      }
      delete data.folder_id
      response = await httpNoteListWithLabel(data).catch(() => null)
    }
    if (showLoading) loading.value = false
    if (!response) return
    const { list, total } = response
    noteList.value = list
    noteListTotal.value = total
  }

  function setTagList(data) {
    tagList.value = data
  }

  function setNoReadNum(data) {
    noReadNum.value = data
  }

  function setWangEditorRef(ref) {
    wangEditorRef.value = ref
  }
  return {
    noteData,
    selectedFolder,
    selectedTag,
    // breadCrumbList,
    shortCutData,
    noteList,
    noteListTotal,
    loading,
    queryNote,
    tagList,
    noReadNum,
    wangEditorRef,
    setSelectedFolder,
    setSelectedTag,
    getFolderListData,
    // setBreadCrumbList,
    getShortCutList,
    getList,
    setTagList,
    setNoReadNum,
    setWangEditorRef,
  }
})

export default useNoteStore
